<template>
    <n-spin size="large" :show="loading">
        <n-form ref="formRef" label-placement="left" :show-feedback="false">
            <n-space vertical :size="24">
                <n-grid :cols="24" :x-gap="24">
                    <n-form-item-gi :span="6" label="城市">
                        <City v-model:value="city"></City>
                    </n-form-item-gi>
                    <n-form-item-gi :span="3" label="状态" v-if="init">
                        <n-switch :loading="loading" v-model:value="conf.operate">
                            <template #checked>营运</template>
                            <template #unchecked>停运</template>
                        </n-switch>
                    </n-form-item-gi>
                    <n-form-item-gi :span="9" label="结算时间" v-if="init">
                        <n-space justify="center">
                            <n-select
                                :style="{ width: '120px' }"
                                size="small"
                                :options="[
                                    { label: '交车后', value: 1 },
                                    { label: '还车后', value: 2 },
                                ]"
                                v-model:value="conf.settle.timing"
                            />
                            <DecimalInput v-model:base="conf.settle.delay" :min="0" suffix="天" />
                            <span>内结算，0表示实时</span>
                        </n-space>
                    </n-form-item-gi>
                    <n-form-item-gi :span="5" v-if="init">
                        <n-space>
                            <n-button @click="doCopy">复制</n-button>
                            <n-button :disabled="!paste" @click="doPaste">粘贴</n-button>
                            <n-button type="primary" :loading="loading" @click="execSaveConfig">保存</n-button>
                        </n-space>
                    </n-form-item-gi>
                </n-grid>
                <n-scrollbar :style="{ maxHeight: height }" v-if="init">
                    <n-grid :cols="24" :x-gap="24">
                        <n-gi :span="12">
                            <n-space vertical>
                                <n-card size="small" title="增值服务收费标准">
                                    <n-space vertical :size="12">
                                        <n-form-item label="送车上门">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.onsite.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                <DecimalInput :precision="3" suffix="km" v-model:base="conf.service.onsite.meter" :min="0" />
                                                内
                                                <DecimalInput :precision="2" suffix="元" v-model:base="conf.service.onsite.cost" :min="0" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.onsite.range" />
                                                ），超出后每公里
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.onsite.min" />
                                                -
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.onsite.max" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="异地还车">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.remote.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                <DecimalInput :precision="3" suffix="km" :min="0" v-model:base="conf.service.remote.meter" />
                                                内
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.remote.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.remote.range" />
                                                ），超出后每公里
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.remote.min" />
                                                -
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.remote.max" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="基础保险">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.bins.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                每天收取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.bins.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.bins.range" />
                                                ） ，每订单最多收取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.bins.max" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="车损保险">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.dins.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                每天收取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.dins.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.dins.range" />
                                                ） ，每订单最多收取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.dins.max" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="三者保险">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.tins.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                <DecimalInput suffix="万元" :min="0" v-model:base="conf.service.tins.quota" />
                                                三者保额，
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.tins.cost" />
                                                /十五天 （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.tins.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="车损免赔">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.free.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.free.quota" />
                                                以内，
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.free.cost" />
                                                /七天 （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.free.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="夜间服务">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.night.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                当日22点~次日7点，每次收费
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.night.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.night.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="免押服务">
                                            <n-space :align="'baseline'" size="small">
                                                <n-switch v-model:value="conf.service.waive.flag">
                                                    <template #checked>开启</template>
                                                    <template #unchecked>关闭</template>
                                                </n-switch>
                                                微信/支付宝信用免押功能
                                            </n-space>
                                        </n-form-item>
                                    </n-space>
                                </n-card>
                                <n-card size="small" title="增值服务抽成">
                                    <n-space vertical :size="12">
                                        <n-form-item label="送车上门">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.onsite.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.onsite.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="异地还车">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.remote.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.remote.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="基础保险">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.bins.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.bins.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="车损保险">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.dins.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.dins.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="三者保险">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.tins.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.tins.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="车损免赔">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.free.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.free.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="夜间服务">
                                            <n-space :align="'baseline'" size="small">
                                                抽取服务费的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.service.night.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.service.night.must" />
                                            </n-space>
                                        </n-form-item>
                                    </n-space>
                                </n-card>
                            </n-space>
                        </n-gi>
                        <n-gi :span="12">
                            <n-space vertical>
                                <n-card size="small" title="租金抽成设置">
                                    <n-space vertical :size="12">
                                        <n-form-item label="直接出租车辆">
                                            <n-space :align="'baseline'" size="small">
                                                抽取订单租金的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.settle.direct.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.settle.direct.must" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="平台出租车辆">
                                            <n-space :align="'baseline'" size="small">
                                                抽取订单租金的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.settle.plat.rate" />
                                                ，最低抽取
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.settle.plat.must" />
                                            </n-space>
                                        </n-form-item>
                                    </n-space>
                                </n-card>
                                <n-card size="small" title="合同相关数据">
                                    <n-space vertical :size="12">
                                        <n-form-item label="代办扣分" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                每分收费
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.contract.point.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.contract.point.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="罚款代缴" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                每次收费
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.contract.fine.cost" />
                                                （±
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.contract.fine.range" />
                                                ），单个订单内所有违章收费一次
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="发票税费" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                开票金额的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.invoice.min" />
                                                至
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.invoice.max" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="维修费标准" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                以
                                                <!-- <n-input :style="largeStyle" v-model:value="conf.contract.unkeep.info" size="small" :bordered="false" placeholder="请填写" /> -->
                                                <n-select
                                                    :style="largeStyle"
                                                    v-model:value="conf.contract.unkeep.info"
                                                    size="small"
                                                    :bordered="false"
                                                    placeholder="请选择"
                                                    :options="[
                                                        { label: '就近4S店', value: '就近4S店' },
                                                        { label: '保险公司', value: '保险公司' },
                                                    ]"
                                                ></n-select>
                                                报价为准
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="车辆停运费" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                基础价格的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.down.per" />
                                                （±
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.down.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="事故折损费" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                维修费用的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.wreck.per" />
                                                （±
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.wreck.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="超时还车计费" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                超时
                                                <DecimalInput suffix="小时" :min="0" :max="23" v-model:base="conf.contract.timeout.half" />
                                                收取半日租金，超时
                                                <DecimalInput suffix="小时" :min="0" :max="23" v-model:base="conf.contract.timeout.day" />
                                                收取一日租金
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="欠费违约金" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                每天收取所欠金额的
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.late.per" />
                                                （±
                                                <DecimalInput :precision="1" suffix="%" :max="100" :min="0" v-model:base="conf.contract.late.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="欠费收车天数" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.force.day" />
                                                （±
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.force.range" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="燃料价格" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                每升
                                                <DecimalInput :precision="2" suffix="元" :min="0" v-model:base="conf.contract.fuel.cost" />
                                                ，或实时油价
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="违章押金解冻" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.unfreeze.day" />
                                                ，可选范围（
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.unfreeze.min" />
                                                至
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.unfreeze.max" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="违章处理时效" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.violation.day" />
                                                ，可选范围（
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.violation.min" />
                                                至
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.violation.max" />
                                                ）
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="违约诉讼时效" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                <DecimalInput suffix="天" :min="0" v-model:base="conf.contract.lawsuit.day" />
                                            </n-space>
                                        </n-form-item>
                                        <n-form-item label="诉讼法院" label-width="auto">
                                            <n-space :align="'baseline'" size="small">
                                                <n-input :style="largeStyle" v-model:value="conf.contract.court.info" size="small" :bordered="false" placeholder="合同签订地" />
                                            </n-space>
                                        </n-form-item>
                                    </n-space>
                                </n-card>
                            </n-space>
                        </n-gi>
                    </n-grid>
                </n-scrollbar>
            </n-space>
        </n-form>
    </n-spin>
</template>

<script lang="ts" setup>
import City from '@/comp/func/City.vue'
import { Http } from '@/libs'
import { useStore } from '@/store'
import { computed, nextTick, ref, watch } from 'vue'

import DecimalInput from '@/comp/func/DecimalInput.vue'

const { $notification: notification } = window

const largeStyle = { width: '180px', textAlign: 'center', borderBottom: '1px solid gray' }

const height = computed(() => store.state.browser.innerHeight - 220 + 'px')

const store = useStore()
const loading = computed(() => store.state.loading.flag)

const init = ref<boolean>(false)

const formRef = ref<any>(null)

const conf = ref<any>({})

const city = ref<any>()

watch(city, (nv, _ov) => {
    execLoadConfig(nv)
})

const execLoadConfig = (cityCode: any) => {
    init.value = false
    Http.post('/ops/sys/area/conf/detail', { city: cityCode + '' }).then(resp => {
        if (resp.stat.err > 0) {
            return
        }
        conf.value = resp.data
        conf.value.contract.court.info = conf.value.contract.court.info || '合同签订地'
        conf.value.contract.lawsuit.day = conf.value.contract.lawsuit.day || 3
        init.value = true
    })
}

const execSaveConfig = () => {
    let model = {
        city: city.value + '',
        ...conf.value,
    }
    Http.post('/ops/sys/area/conf/save', model).then(resp => {
        if (resp.stat.err > 0) {
            return
        }

        notification.success({
            duration: 1500,
            title: '操作成功',
            content: '您已成功城市配置',
        })
    })
}

const copied = ref<any>({})
const paste = computed(() => Object.keys(copied.value).length > 0)

const doCopy = () => {
    copied.value = conf.value
    notification.success({
        duration: 1500,
        title: '操作成功',
        content: '复制配置成功',
    })
}

const doPaste = async () => {
    if (Object.keys(copied.value).length == 0) {
        notification.error({
            duration: 1500,
            title: '无效数据',
            content: '未复制其他配置内容',
        })

        return
    }
    init.value = false
    conf.value = copied.value

    await nextTick()
    init.value = true
}
</script>
